package ma.fizazi.missionsmanagement.business.demandes.impl;

import java.util.Date;
import java.util.List;

import ma.fizazi.missionsmanagement.business.demandes.DemandeBusiness;
import ma.fizazi.missionsmanagement.dao.commons.DAOServices;
import ma.fizazi.missionsmanagement.model.Demande;
import ma.fizazi.missionsmanagement.model.EtapeDemande;
import ma.fizazi.missionsmanagement.model.Information;
import ma.fizazi.missionsmanagement.model.NatureMission;
import ma.fizazi.missionsmanagement.model.TypeDemande;
import ma.fizazi.missionsmanagement.model.TypeMission;
import ma.fizazi.missionsmanagement.model.Valeur;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service("demandeBusiness")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class DemandeBusinessImpl implements DemandeBusiness {

	@Autowired
	DAOServices daoServices;

	public Demande getDemandeById(int id) {
		return (Demande) daoServices.getEntityById2(Demande.class, id);
	}

	public List<Demande> getAllDemande() {
		return daoServices.getEntities(Demande.class);
	}

	@Override
	public List<Demande> getDemandeByType(int id) {
		TypeDemande type = (TypeDemande) daoServices.getEntityById2(TypeDemande.class, id);
		System.out.println("Type Demande : " +type.getTypeDemande());
		return type.getDemandes();
	}

	@Override
	public EtapeDemande getEtapeById(int id) {
		return (EtapeDemande) daoServices.getEntityById2(EtapeDemande.class, id);
	}

	@Override
	public List<EtapeDemande> getEtapesDemande(int id) {
		Demande demande = (Demande) daoServices.getEntityById2(Demande.class, id);
		TypeDemande type = (TypeDemande) daoServices.getEntityById2(TypeDemande.class, demande.getTypeDemande().getId());
		System.out.println("Type Demande : " +type.getTypeDemande());
		return type.getEtapeDemandes();
	}

	@Override
	public List<Information> getInformationByEtape(int id) {
		EtapeDemande etape = (EtapeDemande) daoServices.getEntityById2(EtapeDemande.class, id);
		return etape.getInformations();
	}

	@Override
	public Valeur getInformationValue(int id) {
		Information info = (Information) daoServices.getEntityById2(Information.class, id);
		return info.getValeur();
	}

	@Override
	public List<Valeur> getValeurByDemande(int id) {
		Demande info = (Demande) daoServices.getEntityById2(Demande.class, id);
		return info.getValeurs();
	}

	@Override
	public void saveDemande(Demande demande) {
		daoServices.saveEntity(demande);
		
	}

	@Override
	public void setValueToInformation(int informationId, Valeur valeur) {
		Information info = (Information) daoServices.getEntityById2(Information.class, informationId);
		info.setValeur(valeur);
		info.setDateMaj(new Date());
		daoServices.updateEntity(info);
	}

	@Override
	public List<NatureMission> getAllNatureDemande() {
		return daoServices.getEntities(NatureMission.class);	
	}

	@Override
	public NatureMission getNatureDemandeById(int id) {
		NatureMission info = (NatureMission) daoServices.getEntityById2(NatureMission.class, id);
		return info;
	}

	@Override
	public List<TypeMission> getAllTypeDemandeById() {
		return daoServices.getEntities(TypeMission.class);
	}


}
